package com.kstech.exccheck.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.kstech.exccheck.R;
import com.kstech.exccheck.config.vo.ResourceVO.MsgVO;
import com.kstech.exccheck.config.vo.RealTimeParamVO;
import com.kstech.exccheck.util.AppContext;
import com.kstech.exccheck.util.ViewTemplateUtil;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kstech.exccheck.receiver.ReceivedDataKey.REAL_PARAM_DATA;

/**
 * The type Real time param receiver. 接受指定广播 将数据显示到实时显示组件上
 */
public class RealTimeParamReceiver extends BroadcastReceiver implements ReceivedDataKey{
    private Map<String,TextView> viewMap = new HashMap<String,TextView>();

    @SuppressWarnings({ "unchecked" })
    @Override
    public void onReceive(Context arg0, Intent intent) {
        if(intent.getBooleanExtra("null",false)){
            reSetView();
            return;
        }
        Map<String,String> dataMap = (Map<String,String>)intent.getSerializableExtra(REAL_PARAM_DATA);
        MsgVO msgVO = (MsgVO)intent.getSerializableExtra(ERROR_MSG);
        for(Entry<String, String> entry :dataMap.entrySet()){
            TextView realTimeValue = viewMap.get(entry.getKey());
            if(realTimeValue!=null){
                if (null == entry.getValue() || "".equals(entry.getValue())) {
                    realTimeValue.setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
                } else {
                    realTimeValue.setText(String.valueOf(entry.getValue()));
                }
                if (msgVO != null) {
                    realTimeValue.setTextColor(AppContext.context.getResources().getColor(R.color.red));
                } else {
                    realTimeValue.setTextColor(AppContext.context.getResources().getColor(R.color.black));
                }
            }
        }
    }

    /**
     * Init table.初始化表格布局 太复杂 无意义 可更换为{@linkplain android.widget.GridView} 进行展示
     *
     * @param realTimeParamBody the real time param body
     */
    public void initTable(TableLayout realTimeParamBody) {
        // 首先移除所有视图，避免重叠多次
        realTimeParamBody.removeAllViews();
        // 获取实时配置参数
        List<RealTimeParamVO> realTimeParamConfig = AppContext.getModelFile().getRealTimeParamList();
        // 获取表格模板
        ViewTemplateUtil vtu = new ViewTemplateUtil(AppContext.context);
        TableLayout realTimeTableLayout = vtu.getRealTimeTableLayout();
        TableRow tableRow0 = (TableRow) realTimeTableLayout.getChildAt(0);
        TableRow tableRow1 = (TableRow) realTimeTableLayout.getChildAt(1);

        for (int i = 0; i < realTimeParamConfig.size() && i < 14; i++) {
            TableLayout tl = null;
            if (i < 7) {
                tl = (TableLayout) tableRow0.getChildAt(i);
            } else {
                tl = (TableLayout) tableRow1.getChildAt(i - 7);
            }
            TableRow tr = (TableRow) tl.getChildAt(0);
            TextView nameTv = (TextView) tr.getChildAt(0);
            nameTv.setText(realTimeParamConfig.get(i).getName());
            TextView unitTv = (TextView) tr.getChildAt(1);
            unitTv.setText(realTimeParamConfig.get(i).getUnit());

            TableRow trV = (TableRow) tl.getChildAt(1);
            TextView valueTv = (TextView) trV.getChildAt(0);
            valueTv.setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
            viewMap.put(realTimeParamConfig.get(i)
                    .getName(), valueTv);
        }

        realTimeTableLayout.removeView(tableRow0);
        realTimeTableLayout.removeView(tableRow1);

        realTimeParamBody.addView(tableRow0);
        realTimeParamBody.addView(tableRow1);
    }

    /**
     * reset view
     */
    public void reSetView(){
        List<RealTimeParamVO> realTimeParamConfig = AppContext.getModelFile().getRealTimeParamList();
        for (RealTimeParamVO real:realTimeParamConfig) {
            TextView realTimeValue = viewMap.get(real.getName());
            realTimeValue.setText(AppContext.context.getResources().getString(R.string.noRealTimeData));
            realTimeValue.setTextColor(Color.BLACK);
        }
    }
}
